﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;
using QLKH.PopUp.vattu;

namespace QLKH.View.Vattu
{
    public partial class dsTuyen : UserControl
    {
        DataView view = new DataView();
        public dsTuyen()
        {
            InitializeComponent();
        }

        private void dsTuyen_Load(object sender, EventArgs e)
        {
            clTuyen clt = new clTuyen();
            DataTable dttuyen = clt.danhsachtuyen();

            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = view = dttuyen.DefaultView;
        }

        public void Themtuyen(int outid)
        {
            clTuyen clt = new clTuyen();
            DataTable dttuyen = clt.danhsachtuyen();

            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = view = dttuyen.DefaultView;
        }


        public void Suatuyen(int id)
        {
            clTuyen clt = new clTuyen();
            DataTable dttuyen = clt.danhsachtuyen();

            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = view = dttuyen.DefaultView;
        }

        private void dataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            lbrowindex.Text = e.RowIndex.ToString();
        }

        private void tool_add_Click(object sender, EventArgs e)
        {
            popThemTuyen themt = new popThemTuyen();

            themt.MyFunction = new popThemTuyen.Themtuyen(Themtuyen);
            themt.ShowDialog();
        }

        private void tool_edit_Click(object sender, EventArgs e)
        {
            int row = int.Parse(lbrowindex.Text.Trim());
            if (row >= 0)
            {
                Int16 id = Int16.Parse(dataGrid.Rows[row].Cells["id"].Value.ToString());

                popSuaTuyen ufsin = new popSuaTuyen(id);
                ufsin.MyFunction = new popSuaTuyen.SuaTuyen(Suatuyen);
                ufsin.ShowDialog();
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn tuyến");
            }
        }

        private void tool_delete_Click(object sender, EventArgs e)
        {
            int row = int.Parse(lbrowindex.Text.Trim());
            if (row >= 0)
            {
                Int16 id = Int16.Parse(dataGrid.Rows[row].Cells["id"].Value.ToString());
                clTuyen cltuyen = new clTuyen();

                int outid = cltuyen.delete_tuyen(id);
                if (outid >= 0)
                {
                    DataTable dtt = cltuyen.danhsachtuyen();

                    dataGrid.AutoGenerateColumns = false;
                    dataGrid.DataSource = view = dtt.DefaultView;
                }
                else
                    MessageBox.Show("Bạn không thể xóa");
            }
            else
                MessageBox.Show("Bạn chưa chọn tuyến");
            }
        }
    }
